/*路由守卫*/
import router from '@/router'
//路由列表
import routerList from '@/assets/utils/routerList';
//element 提示框
import {Message} from 'element-ui';

import Vue from "vue";

let Vm = new Vue()
import store from '@/store'
//在跳转之前
router.beforeEach((to, from, next) => {
    //title ico图标
    window.document.title = "中盛供应链";
    // let newLink = document.getElementsByTagName('link');
    // newLink[0].outerHTML = "<link rel='shortcut icon' href='https://gd-hbimg.huaban.com/91835998656e77b9d6486f86ec2e98c1ff97b59212df0-xQRcxt_fw658'>"
    if (to.name) {
        if (to.meta.authority.indexOf(100) != -1 || to.meta.authority.indexOf(Number(window.localStorage.getItem('currentPermissions'))) != -1) {
            if (window.sessionStorage.getItem('tabsViewArray')) {
                let tabsViewArray = JSON.parse(window.sessionStorage.getItem('tabsViewArray'));
                let whetherTabs = false;
                for (let i = 0; i < tabsViewArray.length; i++) {
                    if (tabsViewArray[i].fullPath === to.fullPath) {
                        whetherTabs = true
                    }
                }
                if (!whetherTabs) {
                    tabsViewArray.push({name: to.name, path: to.path, query: {}, fullPath: to.fullPath, title: to.meta.title, iconCloseShow: true})
                    Vm.resetSetItem('tabsViewArray', JSON.stringify(tabsViewArray));
                }
            }
        }
        //缓存
        if (!store.state.cachedViews.includes(to.name)) {
            store.state.cachedViews.push(to.name);
        }
    }

    // 登录页面放行 不在左侧的放行
    if (to.path == '/login' || to.meta.authority.indexOf(0) != -1) {
        next();
        //权限为100的放行  左侧都可以访问
    } else if (to.meta.authority.indexOf(100) != -1) {
        routerList.saveList();
        next();
        //符合权限的放行
    } else if (to.meta.authority.indexOf(Number(window.localStorage.getItem('currentPermissions'))) != -1) {
        routerList.saveList();
        next();
        //不合符权限的限制，并跳转回login页
    } else {
        Message({
            message: '无权限访问',
            type: 'error',
            duration: 3 * 1000
        })
        next('/login');
    }
})

//在跳转之后
router.afterEach(() => {

})
